<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au">

  <db:article>
    <db:info>
      <db:title>Cost Efficient Vial Combination</db:title>
      <db:subtitle>Calculate Price</db:subtitle>

      <db:copyright>
        <db:year>2013</db:year>
        <db:year>2011</db:year>
        <db:year>2010</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>4.1</db:revnumber>
          <db:date>2013-04-22</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Update pipeline.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>4.0</db:revnumber>
          <db:date>2011-12-20</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Implement algorithm version 8.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>3.2</db:revnumber>
          <db:date>2011-10-19</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Changed price and content to TPUU/vial values.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>3.1</db:revnumber>
          <db:date>2011-08-01</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Fixed mis-spellings of 'infusible'</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>3.0</db:revnumber>
          <db:date>2011-05-03</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Implement version 7 of algorithm specification.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1.1</db:revnumber>
          <db:date>2011-03-03</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed bug in C2 report.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1</db:revnumber>
          <db:date>2011-02-04</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed fees for DPMQ.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2011-01-19</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated for use with PBS XML v2.4.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2010-07-23</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet calculates the price of the most cost efficient vial combination. It is the final stage of the transformations needed to perform the calculation.</db:para>
    <db:para>The source document is the result of the grouping part of the transformation. See also <db:filename>vial-combo-group.xsl</db:filename></db:para>
    <db:para>The result document is an XML description of the DPDA and a sample vial combination that was used to calculate the price. NB. The given vial combination is only used for the price calculation and is not indicative of what vial combination should be used to dispense the infusion. Also, only a single vial combination is included; other vial combinations may give the same DPDA, including use of vials from different manufacturers.</db:para>
    <db:note>
      <db:para>In the stylesheet comments, PUOMP is short for "Per Unit-Of-Measure Price".</db:para>
    </db:note>
  </db:article>

  <xsl:output method='xml' indent='yes'/>

  <db:param>The amount of drug to be dispensed.</db:param>
  <xsl:param name='dosage' select='0'/>

  <db:param>Which set of price values to use. Permitted values are "reimbursement" (default) or "manufacturer".</db:param>
  <xsl:param name='price-type'
    select='"reimbursement"'/>

  <db:param>How chatty to be. Value may be "none" (default), "informational" or "extremely" (debugging).</db:param>
  <xsl:param name='verbose' select='"none"'/>

  <xsl:key name='id'
    match='*'
    use='concat("#", @xml:id)'/>

  <!-- A very large value. Every candidate price will be smaller than this value. -->
  <xsl:variable name='infinity' select='99999999.99'/>

  <xsl:template match='/'>
    <xsl:variable name='pr' select='/pbs:root/pbs:schedule/pbs:listings-list/pbs:prescribing-rule'/>

    <xsl:if test='count($pr) != 1'>
      <xsl:message terminate='yes'>Too many prescribing rules: source document has not been prepared correctly</xsl:message>
    </xsl:if>
    
    <xsl:if test='not($pr/pbs:infusible)'>
      <xsl:message terminate='yes'>PBS item code "<xsl:value-of select='$pr/pbs:code'/>" is not an infusible prescribing rule: unable to continue</xsl:message>
    </xsl:if>

    <pbs:result>
      <xsl:choose>
        <xsl:when test='$price-type = "reimbursement"'>
          <xsl:call-template name='display'>
            <xsl:with-param name='spec'>
              <xsl:call-template name='lcvsc'>
                <xsl:with-param name='D' select='$dosage'/>
                <xsl:with-param name='puomp-groups' select='$pr/pbs:infusible/pbs:pricing/pbs:reimbursement/pbs:prices'/>
              </xsl:call-template>
            </xsl:with-param>
            <xsl:with-param name='dr'
              select='$pr/ancestor::pbs:listings-list/pbs:dispensing-rules-list/pbs:dispensing-rule'/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name='display'>
            <xsl:with-param name='spec'>
              <xsl:call-template name='lcvsc'>
                <xsl:with-param name='D' select='$dosage'/>
                <xsl:with-param name='puomp-groups' select='$pr/pbs:infusible/pbs:pricing/pbs:manufacturer/pbs:prices'/>
              </xsl:call-template>
            </xsl:with-param>
            <xsl:with-param name='dr'
              select='$pr/ancestor::pbs:listings-list/pbs:dispensing-rules-list/pbs:dispensing-rule'/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </pbs:result>
  </xsl:template>

  <db:template>
    <db:para>This template performs the Least-Cost Vial Size Combination calculation. See <db:citetitle>Cost Efficient Vial Size Algorithm</db:citetitle> (version 7) for further details.</db:para>
    <db:para>This template returns multiple values, delimited by spaces. The first value is a price. The remainder are pairs of values; the number of vials followed by an identifier for that vial.</db:para>
  </db:template>
  <xsl:template name='lcvsc'>
    <xsl:param name='D' select='0'/>
    <xsl:param name='puomp-groups' select='/..'/>
    <xsl:param name='pack-contents' select='/..'/>
    <xsl:param name='i' select='-1'/>

    <xsl:call-template name='chat'>
      <xsl:with-param name='message'>lcvsc: <xsl:value-of select='count($puomp-groups)'/> puomp-groups (1st "<xsl:value-of select='$puomp-groups[1]/@xml:id'/>") - <xsl:value-of select='count($pack-contents)'/> pack-contents (1st "<xsl:value-of select='$pack-contents[1]/@xml:id'/>") i = <xsl:value-of select='$i'/></xsl:with-param>
      <xsl:with-param name='level' select='"extremely"'/>
    </xsl:call-template>

    <xsl:choose>
      <xsl:when test='not($puomp-groups) or
                      $D &lt;= 0'>
        <!-- There are no further vial groups or dosage amounts to consider -->
        <xsl:value-of select='concat($infinity, " ")'/>
      </xsl:when>
      <xsl:when test='not($pack-contents)'>

        <!-- first consider exactly filling the dosage, then consider overfilling -->

        <xsl:variable name='exact'>
          <xsl:call-template name='exact-fit'>
            <xsl:with-param name='D' select='$D'/>
            <xsl:with-param name='pack-contents' select='$puomp-groups[1]/pbs:tpp-list'/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:choose>
          <xsl:when test='$exact != ""'>
            <xsl:call-template name='chat'>
              <xsl:with-param name='level' select='"informational"'/>
              <xsl:with-param name='message'>Found exact fit for dosage <xsl:value-of select='$D'/></xsl:with-param>
            </xsl:call-template>

            <xsl:value-of select='$exact'/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name='chat'>
              <xsl:with-param name='level' select='"extremely"'/>
              <xsl:with-param name='message'>Unable to find exact fit for dosage <xsl:value-of select='$D'/></xsl:with-param>
            </xsl:call-template>

            <xsl:call-template name='lcvsc'>
              <xsl:with-param name='D' select='$D'/>
              <xsl:with-param name='puomp-groups' select='$puomp-groups'/>
              <xsl:with-param name='pack-contents' select='$puomp-groups[1]/pbs:tpp-list'/>
              <xsl:with-param name='i' select='ceiling($D div $puomp-groups[1]/pbs:tpp-list[1]/pbs:vial-content)'/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>

      <xsl:when test='$i &lt; 0 or
                      $i = "NaN"'/>

      <xsl:when test='$D &lt; $i * $pack-contents[1]/pbs:vial-content'>
        <xsl:call-template name='chat'>
          <xsl:with-param name='level' select='"informational"'/>
          <xsl:with-param name='message'>Considering vial combination <xsl:value-of select='$i'/> x <xsl:value-of select='key("id", $pack-contents[1]/pbs:tpp-reference[1]/@xlink:href)/../pbs:vial-content'/> <xsl:value-of select='key("id", $pack-contents[1]/pbs:tpp-reference[1]/@xlink:href)/../pbs:vial-content/@unit'/> $<xsl:value-of select='format-number($i * $pack-contents[1]/pbs:tpuu-pharmacy-price/pbs:price/pbs:amount, "0.00")'/> for dosage <xsl:value-of select='$D'/><xsl:value-of select='key("id", $pack-contents[1]/pbs:tpp-reference[1]/@xlink:href)/../pbs:vial-content/@unit'/></xsl:with-param>
        </xsl:call-template>

        <xsl:variable name='C1.price'
          select='$i * $pack-contents[1]/pbs:tpuu-pharmacy-price/pbs:price/pbs:amount'/>
        <xsl:variable name='C1.raw'>
          <xsl:value-of select='$C1.price'/>
          <xsl:text> </xsl:text>
          <xsl:value-of select='$i'/>
          <xsl:text> </xsl:text>
          <xsl:value-of select='$pack-contents[1]/pbs:tpp-reference[1]/@xlink:href'/>
        </xsl:variable>

        <xsl:variable name='rest.raw'>
          <xsl:call-template name='lcvsc'>
            <xsl:with-param name='D' select='$D'/>
            <xsl:with-param name='puomp-groups' select='$puomp-groups'/>
            <xsl:with-param name='pack-contents' select='$pack-contents'/>
            <xsl:with-param name='i' select='$i - 1'/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name='rest.price' select='substring-before($rest.raw, " ")'/>

        <xsl:call-template name='min'>
          <xsl:with-param name='values' select='concat($C1.price, " ", $rest.price)'/>
          <xsl:with-param name='return'>
            <xsl:value-of select='$C1.raw'/>
            <xsl:text>&#xa;</xsl:text>
            <xsl:value-of select='$rest.raw'/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <xsl:variable name='D.prime' select='$D - $i * $pack-contents[1]/pbs:vial-content'/>

        <xsl:call-template name='chat'>
          <xsl:with-param name='level' select='"informational"'/>
          <xsl:with-param name='message'>Using <xsl:value-of select='$i'/> x <xsl:value-of select='key("id", $pack-contents[1]/pbs:tpp-reference[1]/@xlink:href)/../pbs:vial-content'/> <xsl:value-of select='key("id", $pack-contents[1]/pbs:tpp-reference[1]/@xlink:href)/../pbs:vial-content/@unit'/>, now considering dosage shortfall of <xsl:value-of select='$D.prime'/> <xsl:value-of select='$pack-contents[1]/pbs:vial-content/@unit'/></xsl:with-param>
        </xsl:call-template>

        <xsl:variable name='C2.raw'>
          <xsl:choose>
            <xsl:when test='$pack-contents[2]'>
              <xsl:call-template name='lcvsc'>
                <xsl:with-param name='D' select='$D.prime'/>
                <xsl:with-param name='puomp-groups' select='$puomp-groups'/>
                <xsl:with-param name='pack-contents' select='$pack-contents[position() != 1]'/>
                <xsl:with-param name='i' select='ceiling($D.prime div $pack-contents[2]/pbs:vial-content)'/>
              </xsl:call-template>
            </xsl:when>
            <xsl:when test='$puomp-groups[2] and
                            $D.prime * $puomp-groups[2]/@puomp &lt; ceiling($D.prime div $pack-contents[1]/pbs:vial-content) * $pack-contents[1]/pbs:vial-content * $puomp-groups[1]/@puomp'>
              <xsl:call-template name='lcvsc'>
                <xsl:with-param name='D' select='$D.prime'/>
                <xsl:with-param name='puomp-groups' select='$puomp-groups[position() != 1]'/>
              </xsl:call-template>
            </xsl:when>
            <xsl:when test='$puomp-groups[2]'>
              <xsl:call-template name='chat'>
                <xsl:with-param name='level' select='"extremely"'/>
                <xsl:with-param name='message'>Not worth considering vials of puomp of more than <xsl:value-of select='$puomp-groups[1]/@puomp'/> for dosage <xsl:value-of select='$D.prime'/></xsl:with-param>
              </xsl:call-template>

              <xsl:value-of select='$infinity'/>
              <xsl:text> </xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name='chat'>
                <xsl:with-param name='level' select='"extremely"'/>
                <xsl:with-param name='message'>No more vial groups or pack contents to consider</xsl:with-param>
              </xsl:call-template>

              <xsl:value-of select='$infinity'/>
              <xsl:text> </xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name='C2.price' select='substring-before($C2.raw, " ")'/>
        <xsl:variable name='C2.rest' select='substring-after($C2.raw, " ")'/>

        <xsl:variable name='rest.raw'>
          <xsl:choose>
            <xsl:when test='$puomp-groups[2] or $pack-contents[2]'>
              <xsl:call-template name='lcvsc'>
                <xsl:with-param name='D' select='$D'/>
                <xsl:with-param name='puomp-groups' select='$puomp-groups'/>
                <xsl:with-param name='pack-contents' select='$pack-contents'/>
                <xsl:with-param name='i' select='$i - 1'/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name='chat'>
                <xsl:with-param name='level' select='"extremely"'/>
                <xsl:with-param name='message'>No more vials to fill dosage</xsl:with-param>
              </xsl:call-template>

              <xsl:value-of select='$infinity'/>
              <xsl:text> </xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name='rest.price' select='substring-before($rest.raw, " ")'/>

        <xsl:call-template name='min'>
          <xsl:with-param name='values' select='concat($C2.price, " ", $rest.price)'/>
          <xsl:with-param name='return'>
            <xsl:value-of select='$i * $pack-contents[1]/pbs:tpuu-pharmacy-price/pbs:price/pbs:amount + $C2.price'/>
            <xsl:text> </xsl:text>
            <xsl:value-of select='$i'/>
            <xsl:text> </xsl:text>
            <xsl:value-of select='$pack-contents[1]/pbs:tpp-reference[1]/@xlink:href'/>
            <xsl:text> </xsl:text>
            <xsl:value-of select='$C2.rest'/>
            <xsl:text>&#xa;</xsl:text>
            <xsl:value-of select='$rest.raw'/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <db:template>
    <db:para>Step 3 of the algorithm.</db:para>
    <db:para>Using a single vial group, in which all vials have the same PUOMP, see if some combination of vials results in no wastage.</db:para>
  </db:template>
  <xsl:template name='exact-fit'>
    <xsl:param name='D' select='0'/>
    <xsl:param name='pack-contents' select='/..'/>

    <xsl:call-template name='chat'>
      <xsl:with-param name='level' select='"extremely"'/>
      <xsl:with-param name='message'>exact-fit: D="<xsl:value-of select='$D'/>" pack-contents (1st "<xsl:value-of select='$pack-contents[1]/@xml:id'/>")</xsl:with-param>
    </xsl:call-template>

    <xsl:choose>
      <xsl:when test='$D &lt;= 0 or
                      not($pack-contents)'/>

      <xsl:when test='$pack-contents[1]/pbs:vial-content > $D'>
        <xsl:call-template name='exact-fit'>
          <xsl:with-param name='D' select='$D'/>
          <xsl:with-param name='pack-contents'
            select='$pack-contents[position() != 1]'/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test='$D mod $pack-contents[1]/pbs:vial-content = 0'>
        <xsl:variable name='E1.price' select='($D div $pack-contents[1]/pbs:vial-content) * $pack-contents[1]/pbs:tpuu-pharmacy-price/pbs:price/pbs:amount'/>

        <xsl:choose>
          <xsl:when test='count($pack-contents) > 1 and
            string-length(substring-after($pack-contents[1]/../pbs:prices/@puomp, ".")) &gt;= 8'>
            <xsl:variable name='rest'>
              <xsl:call-template name='exact-fit'>
                <xsl:with-param name='D' select='$D'/>
                <xsl:with-param name='pack-contents'
                  select='$pack-contents[position() != 1]'/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:variable name='rest.price' select='substring-before($rest, " ")'/>

            <xsl:choose>
              <xsl:when test='$rest = ""'>
                <xsl:value-of select='$E1.price'/>
                <xsl:text> </xsl:text>
                <xsl:value-of select='$D div $pack-contents[1]/pbs:vial-content'/>
                <xsl:text> </xsl:text>
                <xsl:value-of select='$pack-contents[1]/pbs:tpp-reference[1]/@xlink:href'/>
              </xsl:when>
              <xsl:when test='$rest != "" and
                              $rest.price &lt; $E1.price'>
                <xsl:value-of select='$rest'/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select='$E1.price'/>
                <xsl:text> </xsl:text>
                <xsl:value-of select='$D div $pack-contents[1]/pbs:vial-content'/>
                <xsl:text> </xsl:text>
                <xsl:value-of select='$pack-contents[1]/pbs:tpp-reference[1]/@xlink:href'/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <!-- No need to check the other vial contents -->
            <xsl:value-of select='$E1.price'/>
            <xsl:text> </xsl:text>
            <xsl:value-of select='$D div $pack-contents[1]/pbs:vial-content'/>
            <xsl:text> </xsl:text>
            <xsl:value-of select='$pack-contents[1]/pbs:tpp-reference[1]/@xlink:href'/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name='exact-fit-aux'>
          <xsl:with-param name='D' select='$D'/>
          <xsl:with-param name='pack-contents'
            select='$pack-contents'/>
          <xsl:with-param name='g' select='floor($D div $pack-contents[1]/pbs:vial-content)'/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name='exact-fit-aux'>
    <xsl:param name='D' select='0'/>
    <xsl:param name='g' select='0'/>
    <xsl:param name='pack-contents' select='/..'/>

    <xsl:choose>
      <xsl:when test='not($pack-contents) or
                      $D &lt;= 0 or
                      $g &lt; 0'/>
      <xsl:when test='$g = 0'>
        <xsl:call-template name='exact-fit'>
          <xsl:with-param name='D' select='$D'/>
          <xsl:with-param name='pack-contents'
            select='$pack-contents[position() != 1]'/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name='next'>
          <xsl:call-template name='exact-fit'>
            <xsl:with-param name='D' select='$D - ($pack-contents[1]/pbs:vial-content * $g)'/>
            <xsl:with-param name='pack-contents'
              select='$pack-contents[position() != 1]'/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:choose>
          <xsl:when test='$next != ""'>
            <xsl:value-of select='($pack-contents[1]/pbs:tpuu-pharmacy-price/pbs:price/pbs:amount * $g) + substring-before($next, " ")'/>
            <xsl:text> </xsl:text>
            <xsl:value-of select='$g'/>
            <xsl:text> </xsl:text>
            <xsl:value-of select='$pack-contents[1]/pbs:tpp-reference[1]/@xlink:href'/>
            <xsl:text> </xsl:text>
            <xsl:value-of select='substring-after($next, " ")'/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name='exact-fit-aux'>
              <xsl:with-param name='D' select='$D'/>
              <xsl:with-param name='pack-contents' select='$pack-contents'/>
              <xsl:with-param name='g' select='$g - 1'/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <db:template>
    <db:para>This template constructs the final output document.</db:para>
  </db:template>
  <xsl:template name='display'>
    <xsl:param name='spec'/>
    <xsl:param name='dr' select='/..'/>

    <xsl:call-template name='chat'>
      <xsl:with-param name='level' select='"extremely"'/>
      <xsl:with-param name='message'>display: <xsl:value-of select='$spec'/></xsl:with-param>
    </xsl:call-template>

    <!-- Add in the per-infusion fees to calculate the total Reimbursement DPMQ -->
    <xsl:variable name='combo-price' select='substring-before($spec, " ")'/>
    <xsl:variable name='dispensing-fee'>
      <xsl:choose>
        <xsl:when test='$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dispensing"]'>
          <xsl:value-of select='$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dispensing"]/pbs:amount'/>
        </xsl:when>
        <xsl:otherwise>0.00</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name='wholesale-fee'>
      <xsl:choose>
        <xsl:when test='$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:infusion-wholesale"]'>
          <xsl:value-of select='$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:infusion-wholesale"]/pbs:amount'/>
        </xsl:when>
        <xsl:otherwise>0.00</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name='diluent-fee'>
      <xsl:choose>
        <xsl:when test='$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:diluent"]'>
          <xsl:value-of select='$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:diluent"]/pbs:amount'/>
        </xsl:when>
        <xsl:otherwise>0.00</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name='preparation-fee'>
      <xsl:choose>
        <xsl:when test='$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:infusion-preparation"]'>
          <xsl:value-of select='$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:infusion-preparation"]/pbs:amount'/>
        </xsl:when>
        <xsl:otherwise>0.00</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:element name='{$price-type}'
      namespace='http://schema.pbs.gov.au'>
      <pbs:dpmq>
        <pbs:price dispensing-rule='{/pbs:root/pbs:schedule/pbs:listings-list/pbs:dispensing-rules-list/pbs:dispensing-rule/@about}'>
          <pbs:amount>
            <xsl:value-of select='format-number($combo-price + $dispensing-fee + $wholesale-fee + $diluent-fee + $preparation-fee, "0.00")'/>
          </pbs:amount>
          <xsl:if test='$dispensing-fee > 0.00'>
            <pbs:fee xlink:href='#{$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dispensing"]/@xml:id}'>
              <xsl:value-of select='$dispensing-fee'/>
            </pbs:fee>
          </xsl:if>
          <xsl:if test='$wholesale-fee > 0.00'>
            <pbs:fee xlink:href='#{$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:infusion-wholesale"]/@xml:id}'>
              <xsl:value-of select='$wholesale-fee'/>
            </pbs:fee>
          </xsl:if>
          <xsl:if test='$diluent-fee > 0.00'>
            <pbs:fee xlink:href='#{$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:diluent"]/@xml:id}'>
              <xsl:value-of select='$diluent-fee'/>
            </pbs:fee>
          </xsl:if>
          <xsl:if test='$preparation-fee > 0.00'>
            <pbs:fee xlink:href='#{$dr/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:infusion-preparation"]/@xml:id}'>
              <xsl:value-of select='$preparation-fee'/>
            </pbs:fee>
          </xsl:if>
        </pbs:price>
      </pbs:dpmq>
    </xsl:element>

    <pbs:tpp-list>
      <xsl:call-template name='display-vial-combo'>
        <xsl:with-param name='spec' select='substring-after($spec, " ")'/>
      </xsl:call-template>
    </pbs:tpp-list>
  </xsl:template>
  <xsl:template name='display-vial-combo'>
    <xsl:param name='spec'/>

    <xsl:choose>
      <xsl:when test='normalize-space($spec) = ""'/>
      <xsl:otherwise>
        <xsl:variable name='num' select='substring-before($spec, " ")'/>
        <xsl:variable name='vial'>
          <xsl:choose>
            <xsl:when test='contains(substring-after($spec, " "), " ")'>
              <xsl:value-of select='substring-before(substring-after($spec, " "), " ")'/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select='normalize-space(substring-after($spec, " "))'/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>

        <xsl:if test='$num > 0'>
          <pbs:tpp>
            <pbs:quantity>
              <xsl:value-of select='$num'/>
            </pbs:quantity>

            <pbs:form-strength>
              <xsl:apply-templates select='key("id", $vial)/db:title'/>
            </pbs:form-strength>
          </pbs:tpp>
        </xsl:if>
        <xsl:call-template name='display-vial-combo'>
          <xsl:with-param name='spec' select='substring-after(substring-after($spec, " "), " ")'/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <db:template>
    <db:para>Utility template: this template finds the lowest value from a set of values. Value sets are delimited by a newline character. Within a set values are delimited using spaces.</db:para>
  </db:template>
  <xsl:template name='min'>
    <xsl:param name='values'/>
    <xsl:param name='return'/>

    <xsl:variable name='min-return'>
      <xsl:call-template name='min-aux'>
        <xsl:with-param name='values' select='$values'/>
        <xsl:with-param name='return' select='$return'/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:value-of select='substring-after($min-return, "&#xa;")'/>
  </xsl:template>

  <xsl:template name='min-aux'>
    <xsl:param name='values'/>
    <xsl:param name='return'/>

    <xsl:choose>
      <xsl:when test='normalize-space($values) = ""'>
        <xsl:value-of select='$infinity'/>
        <xsl:text>&#xa;</xsl:text>
      </xsl:when>
      <xsl:when test='contains($values, " ")'>
        <xsl:variable name='first' select='substring-before($values, " ")'/>
        <xsl:variable name='remainder-full'>
          <xsl:call-template name='min-aux'>
            <xsl:with-param name='values' select='substring-after($values, " ")'/>
            <xsl:with-param name='return' select='substring-after($return, "&#xa;")'/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name='remainder' select='substring-before($remainder-full, "&#xa;")'/>
        <xsl:variable name='remainder-return' select='substring-after($remainder-full, "&#xa;")'/>

        <xsl:choose>
          <xsl:when test='$first > $remainder'>
            <xsl:value-of select='$remainder-full'/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select='concat($first, "&#xa;", substring-before($return, "&#xa;"))'/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select='concat($values, "&#xa;", $return)'/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name='chat'>
    <xsl:param name='message'/>
    <xsl:param name='level' select='"extremely"'/>

    <xsl:choose>
      <xsl:when test='$verbose = "none"'/>
      <xsl:when test='$verbose = "informational" and $level != "informational"'/>

      <xsl:otherwise>
        <xsl:message>
          <xsl:value-of select='$message'/>
        </xsl:message>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>
</xsl:stylesheet>

